/*
 * Brian M. Luger
 * Conference Assistant Loader
 * 12/3/2011
 */
package caobject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.GregorianCalendar;

import javax.swing.JOptionPane;

/**
 * Conference Assistant Loader Class
 * 
 * @author Brian M. Luger
 * @version 12/3/2011
 */
public final class CALoader {
	/**
	 * Generated Conf Data Save Location.
	 */
	private final static String DATA_LOC = "ConfData.txt";
	
	/**
	 * Generates test data for use with the Conference
	 * Assistant Program.
	 */
	public static void generateTestData() {
		final Conference conf1 = generateConference(
				new User("PC1", EnumRole.PROGRAMCHAIR), "Conf A");
		final Conference conf2 = generateConference(
				new User("PC2", EnumRole.PROGRAMCHAIR), "Conf B");
		final Conference conf3 = generateConference(
				new User("PC3", EnumRole.PROGRAMCHAIR), "Conf C");
		final Conference conf4 = generateConference(
				new User("PC4", EnumRole.PROGRAMCHAIR), "Conf D");
		
		ArrayList<Conference> confData = 
			new ArrayList<Conference>();
		confData.add(conf1);
		confData.add(conf2);
		confData.add(conf3);
		confData.add(conf4);
		
		final File saveLoc = new File(DATA_LOC);
		
		FileOutputStream fos;
		ObjectOutputStream oos;

		try {
			fos = new FileOutputStream(saveLoc);
			oos = new ObjectOutputStream(fos);
			
			oos.writeObject(confData);
			
			oos.close();
			fos.close();
		} catch (final IOException e) {
			JOptionPane.showMessageDialog(
					null, 
					"[ERROR] - " + e, 
					"Error", 
					JOptionPane.ERROR_MESSAGE);
		}
	}
	
	/**
	 * Loads Conference Data.
	 * 
	 * @param theDataPath Path of the Conference Data.
	 * 
	 * @return Conference Data or <tt>null</tt> if the
	 * Conference Data could not be loaded.
	 */
	@SuppressWarnings("unchecked")
	public static ArrayList<Conference> load(final String theDataPath) {
		final File dataPath = new File(theDataPath);
		if (!dataPath.exists()) {
			generateTestData();
			dataPath.renameTo(new File(DATA_LOC));
		}
		
		FileInputStream fis;
		ObjectInputStream ois;
		ArrayList<Conference> data = null;
		
		try {
			fis = new FileInputStream(dataPath);
			ois = new ObjectInputStream(fis);
			
			data = (ArrayList<Conference>) ois.readObject();
			
			ois.close();
			fis.close();
			
		} catch (final IOException e) {
			JOptionPane.showMessageDialog(
					null, 
					"[ERROR] - " + e, 
					"Error", 
					JOptionPane.ERROR_MESSAGE);
		} catch (final ClassNotFoundException e) {
			JOptionPane.showMessageDialog(
					null, 
					"[ERROR] - " + e, 
					"Error", 
					JOptionPane.ERROR_MESSAGE);
		}
		
		return data;
	}
	
	/**
	 * Save method for saving Conference Data
	 * 
	 * @param theDataPath Path used to save the data.
	 * @param the_confData The data to be saved.
	 */
	public static void save(final String theDataPath,
			final ArrayList<Conference> the_confData) {
		final File dataPath = new File(theDataPath);
		
		FileOutputStream fos;
		ObjectOutputStream oos;
		
		try {
			fos = new FileOutputStream(dataPath);
			oos = new ObjectOutputStream(fos);
			
			oos.writeObject(the_confData);
			
			oos.close();
			fos.close();
			
		} catch (final IOException e) {
			JOptionPane.showMessageDialog(
					null, 
					"[ERROR] - " + e, 
					"Error", 
					JOptionPane.ERROR_MESSAGE);
		} 
	}
	
	/**
	 * Private method used for generating test Data...
	 * 
	 * @param thePC Program Chair
	 * @param confTitle Conference Title
	 * 
	 * @return Conference Object
	 */
	private static Conference generateConference(final User thePC, final String confTitle) {
		// Users
		final User authorOne = 
			new User("Author1", EnumRole.AUTHOR);
		authorOne.addNewRole(EnumRole.REVIEWER);
		final User authorTwo = 
			new User("Author2", EnumRole.AUTHOR);
		final User reviewerOne = 
			new User("Reviewer1", EnumRole.REVIEWER);
		final User reviewerTwo = 
			new User("Reviewer2", EnumRole.REVIEWER);
		
		final Conference conf = new Conference(thePC, confTitle);
		
		// Deadlines
		conf.setDeadline(Conference.AUTHOR, 
				new GregorianCalendar(2012, 1, 11));
		conf.setDeadline(Conference.REVIEWER, 
				new GregorianCalendar(2012, 2, 11));
		
		// Manuscripts
		final Manuscript manOne = 
			new Manuscript(
					authorOne.getUserName(), 
					confTitle + " title1", 
					confTitle + " text1");
		manOne.addReviewer(reviewerOne);
//		manOne.addReview(reviewerOne, 
//				confTitle + " Reviewer1 One");
		
		final Manuscript manTwo = 
			new Manuscript(
					authorTwo.getUserName(), 
					confTitle + " title2", 
					confTitle + " text2");
		manTwo.addReviewer(reviewerTwo);
		manTwo.addReviewer(authorOne);
//		manTwo.addReview(reviewerTwo, 
//				confTitle + "Reviewer2 One");
		
		final Manuscript manThree = 
			new Manuscript(
					authorOne.getUserName(), 
					confTitle + " title3", 
					confTitle + " text3");
		manThree.addReviewer(reviewerOne);
		manThree.addReview(reviewerOne, 
				confTitle + "Reviewer1 Two");
		
		// Add Manuscripts
		conf.addManuscript(manOne);
		conf.addManuscript(manTwo);
		conf.addManuscript(manThree);
		
		// Add Users
		conf.addUser(authorOne);
		conf.addUser(authorTwo);
		conf.addUser(reviewerOne);
		conf.addUser(reviewerTwo);
		
		return conf;
	}
}
